"use strict";
/**
 * @ Author: Yoann Meclot. MSay2
 * @ Created on: 2020-08-30 09:25:59
 * @ Modified on: 2021-06-14 18:37:04
 *
 * @version 1.4
 */
/**
 * Copyright (c) 2020-2021 MSay2 (Yoann Meclot) - @msay2/tspoet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeBlock = void 0;
const Exceptions_js_1 = require("../util/Exceptions.js");
const ArrayList_js_1 = require("../util/list/ArrayList.js");
const StringBuilder_js_1 = require("../util/StringBuilder.js");
const Utils_js_1 = require("../util/Utils.js");
const CodeWriter_js_1 = require("./CodeWriter.js");
const TSType_js_1 = require("./TSType.js");
/**
 * The CodeBlock object allows to you write a coplexe code.
 *
 * @class `CodeBlock`
 * @version 1.3
 */
class CodeBlock {
    constructor(builder) {
        this.mCodes = builder.mCodes;
        this.mInports = builder.mInports;
        this.mAnonymousImpls = builder.mAnonymousImpls;
    }
    /**
     * Add a new code block.
     *
     * @type {CodeBlock.Builder}
     * @since 1.0
     */
    static new() {
        return new CodeBlock.Builder();
    }
}
exports.CodeBlock = CodeBlock;
CodeBlock.Builder = class Builder {
    constructor() {
        this.mCodes = new ArrayList_js_1.ArrayList();
        this.mInports = new ArrayList_js_1.ArrayList();
        this.mAnonymousImpls = new ArrayList_js_1.ArrayList();
    }
    /**
     * Add a code line.
     *
     * @param arg The code line to be added.
     * @param values The values to be cast with the 3 expressions.
     * @example $L, $S, $O, $I
     * @throws {TSPoetException} Check if the statement is undefined or null.
     * @throws {TSPoetException} Check if the values is undefined or null.
     * @type {CodeBlock.Builder}
     * @since 1.0
     */
    addStatement(arg, ...values) {
        let cloneArg = Utils_js_1.Preconditions.checkNotNull(arg, "statement", "string argument");
        Utils_js_1.Preconditions.checkNotNull(values, "values statements", "array or nothing");
        let result = cloneArg;
        let length = cloneArg.length;
        let index = 0;
        for (let i = 0; i < length; i++) {
            let char = cloneArg.charAt(i);
            if (char == '$') {
                if (cloneArg.charAt(i + 1) == 'S')
                    result = result.replace('$S', `"${values[index]}"`);
                if (cloneArg.charAt(i + 1) == 'L')
                    result = result.replace('$L', `${values[index]}`);
                if (cloneArg.charAt(i + 1) == 'O') {
                    let objectName = values[index];
                    result = result.replace('$O', objectName.asType().getName());
                    this.putImport(objectName);
                }
                if (cloneArg.charAt(i + 1) == 'I') {
                    if (!(values[index] instanceof TSType_js_1.TSType))
                        throw new Exceptions_js_1.IllegalStateException("The $I annotation is only used for anonymous implementaion.");
                    this.mAnonymousImpls.add(values[index]);
                    result = result.replace("$I", `{$I}`);
                }
                index++;
            }
        }
        let builder = new StringBuilder_js_1.StringBuilder();
        builder.append(result).append((result == "" ? "" : CodeWriter_js_1.CodeWriter.SEMICOLON));
        this.mCodes.add(builder.toString());
        return this;
    }
    /**
     * Add a new code block.
     *
     * @param codeBlock The new code block.
     * @throws {TSPoetException} Check if the code block is undefined or null.
     * @type {CodeBlock.Builder}
     * @since 1.0
     */
    addCodeBlock(codeBlock) {
        this.mCodes.addAll(codeBlock.mCodes);
        this.mInports.addAll(codeBlock.mInports);
        this.mAnonymousImpls.addAll(codeBlock.mAnonymousImpls);
        return this;
    }
    /**
     * Add opening brace and the code line.
     *
     * @param arg The code line to be added.
     * @param values The values to be cast with the 3 expressions.
     * @example $L, $S, $O, $I
     * @throws {TSPoetException} Check if the values is undefined or null.
     * @type {CodeBlock.Builder}
     * @since 1.0
     */
    beginBrace(arg, ...values) {
        let cloneArg = Utils_js_1.Preconditions.checkNotNull(arg, "statement", "string argument");
        Utils_js_1.Preconditions.checkNotNull(values, "values statements", "array or nothing");
        let result = cloneArg;
        let length = cloneArg.length;
        let index = 0;
        if (values.length > 0) {
            for (let i = 0; i < length; i++) {
                let char = cloneArg.charAt(i);
                if (char == "$") {
                    if (cloneArg.charAt(i + 1) == "S")
                        result = result.replace("$S", `"${values[index++]}"`);
                    if (cloneArg.charAt(i + 1) == "L")
                        result = result.replace("$L", `${values[index++]}`);
                    if (cloneArg.charAt(i + 1) == "O") {
                        let objectName = values[index];
                        result = result.replace("$O", objectName.asType().getName());
                        this.putImport(objectName);
                    }
                }
            }
        }
        let builder = new StringBuilder_js_1.StringBuilder();
        builder.append(result);
        this.mCodes.add(builder.toString());
        this.mCodes.add(CodeWriter_js_1.CodeWriter.BEGIN_BRACE);
        return this;
    }
    /**
     * Add opening and closing braces and the code line.
     *
     * @param arg The code line to be added.
     * @param values The values to be cast with the 3 expressions.
     * @example $L, $S, $O, $I
     * @throws {TSPoetException} Check if the values is undefined or null.
     * @type {CodeBlock.Builder}
     * @since 1.1
     */
    nextBrace(arg, ...values) {
        let cloneArg = Utils_js_1.Preconditions.checkNotNull(arg, "statement", "string argument");
        Utils_js_1.Preconditions.checkNotNull(values, "values statements", "array or nothing");
        this.mCodes.add(CodeWriter_js_1.CodeWriter.END_BRACE);
        let result = cloneArg;
        let length = cloneArg.length;
        let index = 0;
        if (values.length > 0) {
            for (let i = 0; i < length; i++) {
                let char = cloneArg.charAt(i);
                if (char == "$") {
                    if (cloneArg.charAt(i + 1) == "S")
                        result = result.replace("$S", `"${values[index++]}"`);
                    if (cloneArg.charAt(i + 1) == "L")
                        result = result.replace("$L", `${values[index++]}`);
                    if (cloneArg.charAt(i + 1) == "O") {
                        let objectName = values[index];
                        result = result.replace("$O", objectName.asType().getName());
                        this.putImport(objectName);
                    }
                }
            }
        }
        let builder = new StringBuilder_js_1.StringBuilder();
        builder.append(result);
        this.mCodes.add(builder.toString());
        this.mCodes.add(CodeWriter_js_1.CodeWriter.BEGIN_BRACE);
        return this;
    }
    /**
     * Add closing brace.
     *
     * @param semicolon Insert a semicolon.
     * @type {CodeBlock.Builder}
     * @since 1.0
     */
    endBrace(semicolon) {
        this.mCodes.add((semicolon ? CodeWriter_js_1.CodeWriter.END_BRACE_SEMICOLON : CodeWriter_js_1.CodeWriter.END_BRACE));
        return this;
    }
    /**
     * Add a new comment.
     *
     * @param arg Your comment.
     * @throws {TSPoetException} Check if the comment is undefined or null.
     * @type {CodeBlock.Builder}
     * @since 1.2
     */
    addComment(arg) {
        this.mCodes.add(`// ${Utils_js_1.Preconditions.checkNotNull(arg, "comment", "comment arg")}`);
        return this;
    }
    /**
     * Build a code block.
     *
     * @type {CodeBlock}
     * @since 1.0
     */
    build() {
        return new CodeBlock(this);
    }
    putImport(objctName) {
        this.mInports.add(objctName);
    }
};
//# sourceMappingURL=CodeBlock.js.map